//-----------------------------------------------------------------------------
//	File: rtc.c
//	Author: hsia
//		Using HSE, 8M/128=62.5kHz, T=16us
//-----------------------------------------------------------------------------

#include "rtc.h"

//-----------------------------------------------------------------------------
//	configure rtc module
//-----------------------------------------------------------------------------
void configure_rtc_module(void)
{
	// enable PWR and BKP clocks
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
	// allow access to BKP domain
	PWR_BackupAccessCmd(ENABLE);
	// reset backup domain
	BKP_DeInit();
	// select LSE as RTC clock source
	RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
	// enable RTC clock
	RCC_RTCCLKCmd(ENABLE);
	// wait for RTC registers synchronization
	RTC_WaitForSynchro();
	// wait until last write operation on RTC registers has finished
	RTC_WaitForLastTask();
	// Set RTC prescaler: set RTC period to 16us
	// RTC period = RTCCLK/RTC_PR = (62.5 KHz)/(0+1)
	RTC_SetPrescaler(0);
	// wait until last write operation on RTC registers has finished
	RTC_WaitForLastTask();
}

//-----------------------------------------------------------------------------
//	rtc_get_time - get the minute, second, msecond, usecond from the rtc
//-----------------------------------------------------------------------------
uint32_t rtc_time_get(void)
{
	return RTC_GetCounter() << 4;
}
